package br.facet.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.facet.pojo.Cliente;
import br.facet.utils.HibernateUtils;

public class ClienteDAO {

	public String salvarCliente(Cliente cliente){

		Session sessao = null;
		Transaction tx = null;
		String retorno  = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();

			if(obterClientePorCPF(cliente.getCpf()).equalsIgnoreCase("inexistente")){
				sessao.saveOrUpdate(cliente);
				tx.commit();
				retorno = "sucesso";

			}
			else{
				retorno = "erro";
			}

		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			if(sessao != null){
				try {
					sessao.close();					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
		return retorno;

	}

	public String obterClientePorCPF(String cpf){

		Session sessao = null;
		Transaction tx = null;
		String retorno = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			Query sql = sessao.createSQLQuery("Select * from tbl_cliente where cpf = :cpf").setParameter("cpf", cpf);

			if(sql.uniqueResult() == null){
				retorno = "inexistente";
			}
			else{

				retorno ="existe";
			}

			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			retorno = "erro";
		} finally {
			if(sessao != null){
				try {
					sessao.close();					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}

		}
		return retorno;
	}
	
	@SuppressWarnings("unchecked")
	public List<Cliente> listByName(String name){

		Session sessao = null;

		try {
			sessao = HibernateUtils.getSession();
			Query query = sessao.createQuery("select c from Cliente c where upper(c.nome) like :nome").setParameter("nome", "%" + name.toUpperCase() + "%");
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(sessao != null){
				try {
					sessao.close();					
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}

		}
		return Collections.<Cliente>emptyList();
	}

	@SuppressWarnings("unchecked")
	public List<Cliente> listar() {

		List<Cliente> lista = new ArrayList<Cliente>();
		Session sessao = null;
		Transaction tx = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			lista = sessao.createCriteria(Cliente.class).list();

		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			if (sessao != null) {
				try {
					sessao.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}

		return lista;
	}	
	
	public Cliente load(Integer id)  {
		Session sessao = null;
		Transaction tx = null;
		Cliente result = null;

		try {
			sessao = HibernateUtils.getSession();
			tx = sessao.beginTransaction();
			result = (Cliente) sessao.load(Cliente.class, id);
		} catch (Exception e) {
			tx.rollback();
		} finally {
			sessao.close();
		}
		return result;
	}
}
